package cn.com.shopec.erp.product.dao.impl;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.Transformers;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.stereotype.Repository;

import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.orm.impl.HibernateEntityDao;
import cn.com.shopec.erp.common.utils.StringUtil;
import cn.com.shopec.erp.product.dao.IProdSupplierDao;
import cn.com.shopec.erp.product.entity.ProdSupplier;
import cn.com.shopec.erp.product.vo.ProdSupplierVo;
import cn.com.shopec.erp.product.vo.ProductVo1;
import cn.com.shopec.erp.purchase.entity.PurchaseReturnOrder;

/**
 * 
 * @ClassName: ProdSupplierDaoImpl
 * @Description: 货品与供应商的关系表
 * @author: duhui
 * @date 2013-5-29 下午03:57:09
 * 
 */
@Repository
public class ProdSupplierDaoImpl extends HibernateEntityDao<ProdSupplier> implements IProdSupplierDao {
	
	@Override
	public void delectProdSuppliers(String prodNo,String supplierNos){
		try {
			String sql = "update TBL_PROD_SUPPLIER set DELETE_FLAG = "+SZSCMConstant.DELETE_FLAG_YES;
			sql += " where PROD_NO='" + prodNo + "'";
			if(StringUtil.isExist(supplierNos)){
				sql += " and SUPPLIER_NO not in ("+supplierNos+")";
			}
			Query query = this.createSQLQuery(sql);
			query.executeUpdate();
		} catch (HibernateException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void updateProdSuppliers(String oldProdNo,String newProdNo){
		try {
			String sql = "update TBL_PROD_SUPPLIER t set t.prod_no='"+newProdNo+"' where t.prod_no='"+oldProdNo+"'";
			Query query = this.createSQLQuery(sql);
			query.executeUpdate();
		} catch (HibernateException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 根据供应商编号查询货品个数
	 * @param status
	 * @return
	 */
	@Override
	public int getProductsCountBySupNo(ProductVo1 productVo1) {
		DetachedCriteria detachedCriteria = createCriteria();
		detachedCriteria.add(Restrictions.eq("supplierNo", productVo1.getCode()));
		return getCountByCriteria(detachedCriteria);
	}
	
	
	/**
	 * 根据供应商名称查询供应商与货品表的关系表中是否存在
	 */
	@Override
	public ProdSupplierVo getPuNoByPurchaseRequest(String supName) {
		if((!supName.equals("") && supName!=null)){
				StringBuffer sql = new StringBuffer();
				sql.append("select t.ID as id,"
						+ "t.SUPPLIER_NO as supplierNo,"
						+ "t.PROD_NO as prodNo,"
						+ "t.UNTAX_COST_PRICE as untaxCostPrice,"
						+ "t.TAX_COST_PRICE as taxCostPrice,"
						+ "t.GROSS_MARGIN as grossMargin,"
						+ "t.IS_RETURNED_GOODS as isReturnedGoos,"
						+ "t.DELETE_FLAG as deleteFlag,"
						+ "t.IS_SUP_DELIVERY as isSupDelivery,"
						+ "b.supplierName as supplierName"
						+ " from TBL_PROD_SUPPLIER t"
						+ " inner join TBL_SUPPLIER_INFO b on t.SUPPLIER_NO = b.SUP_CODE "
						+ "where b.supplierName = '"+supName+"' ");
				Query query= getSession().createSQLQuery(sql.toString())
						.addScalar("id",StandardBasicTypes.STRING)
						.addScalar("supplierNo",StandardBasicTypes.STRING)
						.addScalar("prodNo",StandardBasicTypes.STRING)
						.addScalar("untaxCostPrice",StandardBasicTypes.DOUBLE)
						.addScalar("taxCostPrice",StandardBasicTypes.DOUBLE)
						.addScalar("grossMargin",StandardBasicTypes.DOUBLE)
						.addScalar("isReturnedGoos",StandardBasicTypes.INTEGER)
						.addScalar("deleteFlag",StandardBasicTypes.INTEGER)
						.addScalar("isSupDelivery",StandardBasicTypes.INTEGER)
						.addScalar("supplierName",StandardBasicTypes.STRING);
				query.setResultTransformer(Transformers.aliasToBean(ProdSupplierVo.class));
				ProdSupplierVo purchase = (ProdSupplierVo) query.uniqueResult();
				return purchase;
		}
		return null;
	}
}
